<?php

namespace App\Admin\Controllers;

use App\Models\Movie;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Str;
class MoviesController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Movie);
        $grid->model()->orderBy('created_at', 'desc');
        $grid->id('ID');
        $grid->column('short_name', '名称');
        $grid->year('年代');
        $grid->column('douban_id', '豆瓣地址')->display(function($val) {
            if ($val > 0 ) return '<a href="https://movie.douban.com/subject/' . $val . '/">在豆瓣查看</a>';
            else return '';
        });
        $grid->cover('海报')->display(function($cover){
            return Str::startsWith($cover, 'http') ? $cover : 'https://pic.touxiangge.com/' . $cover;
        })->image(100,100);
        $grid->introduction('简介');
        $grid->created_at('Created at');
        $grid->updated_at('Updated at');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Movie::findOrFail($id));

        $show->id('ID');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Movie);

        $form->text('name', '名称')->placeholder('名称');
        $form->text('outher_name', '别名')->placeholder('名称');
        $form->textarea('introduction', '简介')->placeholder('简介');
        $form->text('date', '年代')->placeholder('年代');
        $form->text('runtime', '时长')->placeholder('时长');
        $form->number('year', '年代')->placeholder('年代')->max(2200)->min(1800);
        // $form->datetime('updated_at', '最后更新时间');


        return $form;
    }
}
